<?php 
	class ControllerMemoryKnowledgelist extends Controller {
		private $error = array();
		public function index() {
			$this->document->setTitle("knowledgelist");
			$this->document->addStyle('admin/view/stylesheet/bootstrap-datetimepicker.min.css');
			$this->document->addScript('admin/view/javascript/bootstrap-datetimepicker.js');
			$this->document->addScript('admin/view/javascript/bootstrap-datetimepicker.zh-CN.js');
			$this->load->model('knowledge/knowledge');
			//主体表格数据
			if (isset($this->request->get['page'])) {
				$page = $g_args['page'] = $this->request->get['page'];
			}else{
				$g_args['page'] = 1;
			}
			if (isset($this->request->get['sort'])) {
				$g_args['sort'] = $this->request->get['sort'];
			} else {
				$g_args['sort'] = 'kname';
			}
			if (isset($this->request->get['order'])) {
				$g_args['order'] = $this->request->get['order'];
			} else {
				$g_args['order'] = 'ASC';
			}
			if(isset($this->request->get['kname'])) {
				$g_args['kname'] = $this->request->get['kname'];
			}
			if(isset($this->request->get['label'])) {
				$g_args['label'] = $this->request->get['label'];
			}
			if(isset($this->request->get['pass'])) {
				$g_args['pass'] = $this->request->get['pass'];
			}
			if(isset($this->request->get['complate'])) {
				$g_args['complate'] = $this->request->get['complate'];
			}
			if(isset($this->request->get['starttime'])) {
				$g_args['starttime'] = $this->request->get['starttime'];
			}
			if(isset($this->request->get['endtime'])) {
				$g_args['endtime'] = $this->request->get['endtime'];
			}
			$all = $this->getknowledtelist($g_args);
			$data['knowledges'] = $all['knowledges'];
			$data['pagination'] = $all['pagination'];
			if($this->request->server['REQUEST_METHOD'] == 'POST') {
				//在这里将数据重新加载
				$p_args['sort'] = $this->request->post['sort'];
				$p_args['order'] = $this->request->post['order'];
				$p_args['page ']= $this->request->post['page'];
				$p_args['kname'] = $this->request->post['kname'];
				$p_args['label'] = $this->request->post['label'];
				$p_args['pass'] = $this->request->post['pass'];
				$p_args['complate'] = $this->request->post['complate'];
				$p_args['starttime'] = $this->request->post['starttime'];
				$p_args['endtime'] = $this->request->post['endtime'];
				$results = $this->getknowledtelist($p_args);
				header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($results,0));
			}
			$data['maincontent'] = "知识点列表";
			$data['editknowledge'] = $this->url->link('memory/editknowledge', '', true)."&token=".$this->session->data['token'];
			$data['getdetailurl'] = $this->url->link('memory/knowledgelist/getknowledgedetail','',true)."&token=".$this->session->data['token'];
			$data['repeatkurl'] = $this->url->link('memory/knowledgelist/repeatknowledge','',true)."&token=".$this->session->data['token'];
			$data['removekurl'] = $this->url->link('memory/knowledgelist/removeknowledge','',true)."&token=".$this->session->data['token'];
			$data['eidturl'] = $this->url->link('memory/knowledgelist/editknowledge','',true)."&token=".$this->session->data['token'];
			$data['getlabelurl'] = $this->url->link('memory/knowledgelist/getlabel','',true)."&token=".$this->session->data['token'];
			$data['header'] = $this->load->controller('common/headerin');
	 		$data['footerin'] = $this->load->controller('common/footerin');
	 		$data['footer'] = $this->load->controller('common/footer');
	 		$this->response->setOutput($this->load->view('memory/knowledgelist', $data));
		}
		public function getknowledgedetail() {
			if($this->request->server['REQUEST_METHOD'] == 'POST') {
				$kid = $this->request->post['kid'];
				$this->load->model('complate/complate');
				$results = $this->model_complate_complate->getComplateByKid($kid);
				header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($results,0));
			}
		}
		public function getknowledtelist($args){
			//主体数据
			$this->load->model('knowledge/knowledge');
			$data = array();
			$filter_data = array(
				'sort'      => $args['sort'],
				'order'     => $args['order'],
				'kname'     => $args['kname'],
				'label'     => $args['label'],
				'pass'      => $args['pass'],
				'complate'  => $args['complate'],
				'starttime' => $args['starttime'],
				'endtime'   => $args['endtime'],
				'user_id'   => $this->session->data['user_id'],
				'start'     => ((int)$args['page'] -1)*10,
				'limit'     => 10
			);
			$results = $this->model_knowledge_knowledge->getKnowledgesByUid($filter_data);
			foreach ($results['maindata'] as $result) {
				$data[] = array(
						'kid'     => $result['id'],
						'kname'   => $result['kname'],
						'ftime'   => $result['ftime'],
						'kdetail' => $result['kdetail'],
						'iscomplate'=> $result['iscomplate'],
						'ispass'  => $result['ispass']
					); 
			}
			//构造分页的url
			$url = '';
			if(isset($args['sort'])) {
				$url .= '&sort='.$args['sort'];
			}
			if(isset($args['order'])) {
				$url .= '&order='.$args['order'];
			}
			if(isset($args['kname'])) {
				$url .= '&kname='.$args['kname'];
			}
			if(isset($args['label'])) {
				$url .= '&label='.urlencode($args['label']);
			}
			if(isset($args['pass'])) {
				$url .= '&pass='.$args['pass'];
			}
			if(isset($args['complate'])) {
				$url .= '&complate='.$args['complate'];
			}
			if(isset($args['starttime'])) {
				$url .= '&starttime='.urlencode($args['starttime']);
			}
			if(isset($args['endtime'])) {
				$url .= '&endtime='.urlencode($args['endtime']);
			}
			//分页开始
			$pagination = new Pagination();
			$pagination->total = $results['total'];
			$pagination->page  = $args['page'];
			$pagination->limit = 10;
			$pagination->url = $this->url->link('memory/knowledgelist', 'token=' . $this->session->data['token'] . $url.'&page={page}', true);
			$pagination = $pagination->render();
			//分页结束
			$results = array(
					'knowledges' => $data,
					'pagination' => $pagination
				);
			return $results;
		}
		public function repeatknowledge(){
			if($this->request->server['REQUEST_METHOD'] == 'POST') {
				$this->load->model('knowledge/knowledge');
				$this->load->model('complate/complate');
				$this->load->model('settingreview/settingreview');
				$kid = $this->request->post['kid'];
				$ftime = $this->request->post['ftime'];
				$user_id = $this->session->data['user_id'];
				if($results = $this->model_knowledge_knowledge->repeatFtime($kid,$ftime)){
					$query = $this->model_settingreview_settingreview->getSettingreview($this->session->data['user_id']);
					if(empty($query)){
						$query = $this->config->get("setting_review");
					}
					//调用complatetime类来决定复习时间
					$this->load->library("complatetime");
					$c_time = new complatetime($ftime,$query);
					$complatetime = $c_time->getComplatetime();
					$num = $this->model_complate_complate->updateComplate($complatetime,$kid,$user_id);
				}
				header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($num,0));
			}
		}
		public function removeknowledge(){
			if($this->request->server['REQUEST_METHOD'] == 'POST') {
				$kid = $this->request->post['kid'];
				$this->load->model('knowledge/knowledge');
				$this->load->model('complate/complate');
				if($result = $this->model_knowledge_knowledge->removeKnowledge($kid)){
					$info = $this->model_complate_complate->deleteComplate($kid);
				};
				header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($info,0));
			}
		}
		public function editknowledge(){
			if($this->request->server['REQUEST_METHOD'] == 'POST') {
				$info['kid'] = $this->request->post['kid'];
				$info['kname'] = $this->request->post['kname'];
				$info['kdetail'] = $this->request->post['kdetail'];
				$info['label'] = $this->request->post['label'];
				$info['user_id'] = $this->session->data['user_id'];
				$this->load->model('knowledge/knowledge');
				$results = $this->model_knowledge_knowledge->editKnowledge($info);
				header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($results,0));
            }
		}
		public function getLabel(){
			if($this->request->server['REQUEST_METHOD'] == 'POST') {
				$this->load->model('knowledge/knowledge');
				$kid = $this->request->post['kid'];
				$results = $this->model_knowledge_knowledge->getLabelByKid($kid);
				header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($results,0));
			}
		}
	}
 ?>